package com.wsx.ones.boots.example;

import com.alibaba.fastjson.JSON;
import com.wsx.ones.api.DataService;
import com.wsx.ones.api.ExampleService;
import com.wsx.ones.api.bean.ExampleRequest;
import com.wsx.ones.api.bean.ExampleResponse;
import com.wsx.ones.boots.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangsx
 * @Title: com.turing.chat.test.controller.data.DataController
 * @Description: TODO
 * @date 2019/4/23
 */
@RestController
@RequestMapping("/data")
public class DataController {

    private static Map<String, String> header = new HashMap<>();
    static {
        header.put("content-type", "application/x-protobuf");
        header.put("Accept", "application/x-protobuf");
    }

    @Autowired
    private ExampleService exampleService;

    @GetMapping("/http/{name}")
    public String http(@PathVariable("name") String name, HttpServletResponse servletResponse) {
        ExampleRequest request = new ExampleRequest();
        request.setName(name);
        request.setDate(new Date());
        try {
            String dt = HttpClientUtils.execPost("http://127.0.0.1:8081/docker/http", JSON.toJSONString(request), null);
            ExampleResponse response = JSON.parseObject(dt, ExampleResponse.class);
            return response.getName();
        } catch (Exception e) {
            servletResponse.setStatus(500);
            return "error";
        }
    }

    @GetMapping("/proto/{key}")
    public String proto(@PathVariable("key") String key, HttpServletResponse servletResponse) {
        DataService.Request request  = DataService.Request.newBuilder().setKey(key).setValue("hello world").build();
        try {
            byte[] datas = HttpClientUtils.post("http://127.0.0.1:8081/docker/proto", header, request.toByteArray());
            DataService.Response response = DataService.Response.parseFrom(datas);
            return response.getKey();
        } catch (Exception e) {
            servletResponse.setStatus(500);
            return "error";
        }
    }

    @GetMapping("/dubbo/{name}")
    public String dubbo(@PathVariable("name") String name, HttpServletResponse servletResponse) {
        ExampleRequest request = new ExampleRequest();
        request.setName(name);
        request.setDate(new Date());
        try {
            ExampleResponse response = exampleService.example(request);
            return response.getName();
        } catch (Exception e) {
            servletResponse.setStatus(500);
            return "error";
        }
    }

    @GetMapping("/text/{name}")
    public String text(@PathVariable("name") String name, HttpServletResponse servletResponse) {
        ExampleRequest request = new ExampleRequest();
        request.setName(name);
        request.setDate(new Date());
        String url = "http://127.0.0.1:8081/docker/stream";
        if ("text".equals(name)) {
            url = "http://127.0.0.1:8081/docker/text";
        }
        try {
            String dt = HttpClientUtils.execTextPost(url, JSON.toJSONString(request), null);

            return "success";
        } catch (Exception e) {
            servletResponse.setStatus(500);
            return "error";
        }
    }
}
